﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;

namespace _129_DefaultIfEmpty方法的作用_LINQ语法
{
    class Program
    {
        static void Main(string[] args)
        {
            订单详细信息 d1 = new 订单详细信息
            {
                Amount = 10,
                Price = 2.5M,
                Code = "T-80770"
            };

            订单详细信息 d2 = new 订单详细信息
            {
                Amount = 12,
                Price = 3.2M,
                Code = "T-70778"
            };

            订单列表 o1 = new 订单列表
            {
                ID = 1,
                Date = new DateTime(2018, 3, 1),
                State = true,
                Details = d1
        };

            订单列表 o2 = new 订单列表
            {
                ID=2,
                Date=new DateTime(2018,3,13),
                State=false,
                Details=d2
            };

            订单列表 o3 = new 订单列表
            {
                ID = 3,
                Date = new DateTime(2018, 3, 18),
                State = true,
                Details = null
            };

            List<订单列表> 订单列表s = new List<订单列表> { o1, o2, o3 };
            List<订单详细信息> 订单详细信息s = new List<订单详细信息> { d1, d2 };

            var 查询 = from o in 订单列表s
                     join d in 订单详细信息s on o.Details equals d into g
                     from x in g.DefaultIfEmpty(new 订单详细信息 { Amount = 0, Price = 0.00M, Code = "未知编码" })
                     select (OrderID: o.ID, Amount: x.Amount, Code: x.Code);

            Console.WriteLine("{0,-8}{1,-8}{2,-15}", "订单号", "数量", "编码");
            Console.WriteLine("-----------------------------");
            foreach (var i in 查询)
            {
                Console.WriteLine("{0,-11}{1,-10}{2,-20}", i.OrderID, i.Amount, i.Code);
            }

        }
    }

    public class 订单详细信息
    {
        public int Amount { get; set; }
        public decimal Price { get; set; }
        public string Code { get; set; }
    }

    public class 订单列表
    {
        public int ID { get; set; }
        public DateTime Date { get; set; }
        public bool State { get; set; }
        public 订单详细信息 Details { get; set; }



    }
}
